package com.pkutech.carcasher.network;

import android.util.Log;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by cyy on 2016/6/13.
 */
public class SoapClient {

    public static final String TAG = "SoapClient";
    public static final String URL = "http://222.191.229.250:9080/zntc-ws/services/TransactionWs?wsdl";//天公测试环境
//    public static final String URL = "http://58.214.236.155:18080/zntc-ws/services/TransactionWs?wsdl";//正式环境
//    public static final String URL = "http://58.215.243.232:8081/zntc-ws/services/TransactionWs?wsdl";//感知测试环境

    public static final String NAMESPACE = "http://pkutech.axis2/";

    public static String request(String methodName, Map<String, String> paramsMap){
        try {
            SoapObject request = new SoapObject(NAMESPACE, methodName);
            for (Iterator<Map.Entry<String, String>> it = paramsMap.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                request.addProperty(entry.getKey(), entry.getValue());
            }

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(request);

            HttpTransportSE transport = new HttpTransportSE(URL);

            transport.call(NAMESPACE+methodName, soapEnvelope);
            SoapPrimitive soapPrimitive = (SoapPrimitive) soapEnvelope.getResponse();
            if(soapPrimitive != null){
                return soapPrimitive.toString();
            }
            return null;
        } catch (Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
            return null;
        }
    }

}
